﻿function $f(id) {
    return document.getElementById(id);
}
//-------------------------------------------------------------------------------------
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
//-------------------------------------------------------------------------------------
Array.prototype.findIndex = function(value) {
    var ctr = "";
    for (var i = 0; i < this.length; i++) {
        if (this[i] == value) {
            return i;
        }
    }
    return ctr;
};
//-------------------------------------------------------------------------------------
function addCommas(nStr) {
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}
//-------------------------------------------------------------------------------------
function removeCommas(strValue) //Removes commas from source string.
{
    var objRegExp = /,/g; //search for commas globally
    return strValue.replace(objRegExp, '');
}
//-------------------------------------------------------------------------------------
function findElement_byAttribute(oElm, strTagName, strAttributeName, strAttributeValue) {
    var arrElements = (strTagName == "*" && oElm.all) ? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var oAttributeValue = (typeof strAttributeValue != "undefined") ? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)", "i") : null;
    var oCurrent;
    var oAttribute;
    for (var i = 0; i < arrElements.length; i++) {
        oCurrent = arrElements[i];
        oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
        if (typeof oAttribute == "string" && oAttribute.length > 0) {
            if (typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))) {
                arrReturnElements.push(oCurrent);
            }
        }
    }
    return arrReturnElements;
}
//--------------------------------------------------------------------------------------
function ObjectPosition(obj) {
    var curleft = 0;
    var curtop = 0;

    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft, curtop];
}
//--------------------------------------------------------------------------------------
//utility for resolving relative URLS
//Added by E.Bastani on 1389/11/26
Url = function() { }
Url.prototype =
        {
            _relativeRoot: '<%= ResolveUrl("~/") %>',

            resolve: function(relative) {
                var resolved = relative;
                if (relative.charAt(0) == '~') resolved = this._relativeRoot + relative.substring(2);
                return resolved;
            }
        }
$Url = new Url();
//--------------------------------------------------------------------------------------
function showHelp() {
    var pageName = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1, window.location.pathname.lastIndexOf('aspx') - 1);
    var ProjectName = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/'));
    ProjectName = ProjectName.substring(ProjectName.lastIndexOf('/') + 1);
    var helpURL = $Url.resolve('~/MabnaERPHelp/' + ProjectName + '/' + pageName + '.htm');
    window.open(helpURL);
}
//--------------------------------------------------------------------------------------
function getQueryString(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}
//-----------------------------------------------------------------------------------------
function replaceWildCards(searchstr) {
    if (hasNoWildCard(searchstr)) {
        return "%" + searchstr + "%";
    }
    else {
        searchstr = searchstr.replace('*', '%');
        searchstr = searchstr.replace('?', '_');
        searchstr = searchstr.replace('؟', '_');
        searchstr = searchstr.replace('\"', '');
    }
    return searchstr;
}
//-----------------------------------------------------------------------------------------
function hasNoWildCard(searchstr) {
    for (var i = 0; i < searchstr.length; i++)
        if (searchstr.charAt(i) == '*' || searchstr.charAt(i) == '?' || searchstr.charAt(i) == '؟' || searchstr.charAt(i) == '"')
        return false;
    return true;
}
//--------------------------------------------------------------------------------------------------------
function pad(num, size) {
    var s = num + "";
    while (s.length < size) s = "0" + s;
    return s;
}
//----------------------------------------------------------------------------------------
// written by E.Bastani on 1390/08/29
// find specified elements in a json array where has key/val pair
function getJSONObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getJSONObjects(obj[i], key, val));
        }
        else if (i == key && obj[key].trim() == val) { objects.push(obj); } 
    } return objects;
} 
//-------------------------------------------------------------------------------------------